package com.jintian.smart.kernel.flowable.util;

import cn.hutool.core.date.format.FastDateParser;

import java.text.ParseException;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;

public class DateUtil {
    /**
     * 统计两个时间的时间差 start-end 相差几秒几毫秒
     */
    public static String getDistanceTime(String startTime, String endTime) {
        if (startTime == null || endTime == null) {
            return null;
        }
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date start;
        Date end;
        long diff = 0;// 毫秒差
        String result = "";
        try {
            start = df.parse(startTime);
            end = df.parse(endTime);
            long time1 = start.getTime();
            long time2 = end.getTime();
            diff = time2 - time1;
            // 计算天数
            long days = diff / (24 * 60 * 60 * 1000);
            // 计算小时数
            long hours = (diff / (60 * 60 * 1000)) % 24;
            // 计算分钟数
            long minutes = (diff / (60 * 1000)) % 60;
            // 计算秒数
            long seconds = (diff / 1000) % 60;
            String daystr = days + "天";
            String hourStr = hours + "时";
            String minStr = minutes + "分";
            String secondStr = seconds + "秒";
            if (seconds > 0) {
                if (days != 0) {
                    result = result + daystr;
                }
                if (hours != 0) {
                    result = result + hourStr;
                }
                if (minutes != 0) {
                    result = result + minStr;
                }
                if (seconds != 0) {
                    result = result + secondStr;
                }
            } else {
                result = "已超期";
            }

        } catch (ParseException e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 统计两个时间的时间差 start-end 相差几秒几毫秒
     */
    public static String getDistanceTime(Date start, Date end) {
        if (start == null || end == null) {
            return null;
        }
        long diff = 0;// 毫秒差
        String result = "";
        long time1 = start.getTime();
        long time2 = end.getTime();
        diff = time2 - time1;
        // 计算天数
        long days = diff / (24 * 60 * 60 * 1000);
        // 计算小时数
        long hours = (diff / (60 * 60 * 1000)) % 24;
        // 计算分钟数
        long minutes = (diff / (60 * 1000)) % 60;
        // 计算秒数
        long seconds = (diff / 1000) % 60;
        String daystr = days + "天";
        String hourStr = hours + "时";
        String minStr = minutes + "分";
        String secondStr = seconds + "秒";
        if (seconds > 0) {
            if (days != 0) {
                result = result + daystr;
            }
            if (hours != 0) {
                result = result + hourStr;
            }
            if (minutes != 0) {
                result = result + minStr;
            }
            if (seconds != 0) {
                result = result + secondStr;
            }
        } else {
            result = "已超期";
        }
        return result;
    }

    /**
     * 统计两个时间的时间差 start-end 相差几秒几毫秒
     */
    public static String convertDistanceTime(Long diff) {
        String result = "";
        if (diff != null) {
            // 计算天数
            long days = diff / (24 * 60 * 60 * 1000);
            // 计算小时数
            long hours = (diff / (60 * 60 * 1000)) % 24;
            // 计算分钟数
            long minutes = (diff / (60 * 1000)) % 60;
            // 计算秒数
            long seconds = (diff / 1000) % 60;
            String daystr = days + "天";
            String hourStr = hours + "时";
            String minStr = minutes + "分";
            String secondStr = seconds + "秒";
            if (seconds > 0) {
                if (days != 0) {
                    result = result + daystr;
                }
                if (hours != 0) {
                    result = result + hourStr;
                }
                if (minutes != 0) {
                    result = result + minStr;
                }
                if (seconds != 0) {
                    result = result + secondStr;
                }
            } else {
                result = "已超期";
            }
        }
        return result;
    }


    public static Date parseDate(final String str, final String... parsePatterns) throws ParseException {
        return parseDate(str, null, parsePatterns);
    }

    public static Date parseDate(final String str, final Locale locale, final String... parsePatterns) throws ParseException {
        return parseDateWithLeniency(str, locale, parsePatterns, true);
    }

    private static Date parseDateWithLeniency(final String str, final Locale locale, final String[] parsePatterns,
                                              final boolean lenient) throws ParseException {
        if (str == null || parsePatterns == null) {
            throw new IllegalArgumentException("Date and Patterns must not be null");
        }

        final TimeZone tz = TimeZone.getDefault();
        final Locale lcl = locale != null ? locale : Locale.getDefault();
        final ParsePosition pos = new ParsePosition(0);
        final Calendar calendar = Calendar.getInstance(tz, lcl);
        calendar.setLenient(lenient);

        for (final String parsePattern : parsePatterns) {
            final FastDateParser fdp = new FastDateParser(parsePattern, tz, lcl);
            calendar.clear();
            try {
                if (fdp.parse(str, pos, calendar) && pos.getIndex() == str.length()) {
                    return calendar.getTime();
                }
            } catch (final IllegalArgumentException ignore) {
                // leniency is preventing calendar from being set
            }
            pos.setIndex(0);
        }
        throw new ParseException("Unable to parse the date: " + str, -1);
    }


    public static void main(String[] args) {
        System.out.println(convertDistanceTime(33548596L));
    }
}
